﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM.Business
{
    public class NewList
    {
        /// <summary>
        /// 获取一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataLayer.ContentInfor GetNewById(int id)
        {
            var db = new CRM.DataLayer.CRMDateEntities();
            var result = db.ContentInfor.SingleOrDefault(p => p.ID == id);

            if (result != null)
            {
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 删除一条新闻记录
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public static bool DeleteNews(DataLayer.ContentInfor news)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {                    
                    var result = db.ContentInfor.SingleOrDefault(p => p.ID == news.ID);
                    if (result != null)
                    {
                        db.ContentInfor.DeleteObject(result);
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return false;
            }


        }
        /// <summary>
        /// 修改一条新闻记录
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>

        public static bool ModifyNews(DataLayer.ContentInfor news)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = db.ContentInfor.SingleOrDefault(p => p.ID == news.ID);
                    if (result != null)
                    {
                        result.title = news.title;
                        result.author = news.author;
                        result.content = news.content;
                        result.image = news.image;
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }

                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 增加新闻
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public static bool AddNews(DataLayer.ContentInfor news)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    db.ContentInfor.AddObject(news);
                    int changes = db.SaveChanges();
                    if (changes > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 后台显示信息列表
        /// </summary>
        /// <param name="totalCount">获取总记录数目</param>
        /// <param name="pageNum">总页数</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public static IList<CRM.DataLayer.ContentInfor> ManageListNews(out int totalCount, int pageNum, int pageSize, string type)
        {
            try
            {
                using (CRM.DataLayer.CRMDateEntities db = new CRM.DataLayer.CRMDateEntities())
                {
                    var result = from news in db.ContentInfor
                                 orderby news.date descending
                                 where news.type == type
                                 select news;
                    //获取总记录数目
                    totalCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch
            {
                totalCount = 0;
                return null;
            }

        }
    }
}
